package com.artic.drools;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.*;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;

import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: czarnik_a
 * Date: 27.01.11
 * Time: 09:50
 * To change this template use File | Settings | File Templates.
 */
public class DroolsTest {
    private static DroolsTest ourInstance = new DroolsTest();

    private KnowledgeBase kbase;

    public static DroolsTest getInstance() {
        return ourInstance;
    }

    private DroolsTest() {
        try {
            this.readKnowledgeBase();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private  void readKnowledgeBase() throws Exception {
          KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newInputStreamResource( getClass().getResourceAsStream( "licenseApplication.drl" ) ),ResourceType.DRL );
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
          if (errors.size() > 0) {
               for (KnowledgeBuilderError error: errors) {
                    System.err.println(error);
               }
               throw new IllegalArgumentException("Could not parse knowledge.");
          }
          kbase = KnowledgeBaseFactory.newKnowledgeBase();
          kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
     }

    public KnowledgeBase getKbase() {
        return kbase;
    }

    public void setKbase(KnowledgeBase kbase) {
        this.kbase = kbase;
    }
}
